/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'id',
        title: 'ID',
        type: 'number',
        columnClass: 'text-center width-100',
        fastSort: false

    },
    {
        id: 'userId',
        title: '用户ID',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq'
    },
    {
        id: 'realname',
        title: '真实姓名',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'idcard',
        title: '身份证号',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'authStatus',
        title: '认证状态',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable:{
            1:$lang.realNameStatus.normal,
            2:$lang.realNameStatus.certificated,
            3:$lang.realNameStatus.failed
        }
    },
    {
        id: 'type',
        title: '认证方式',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable:{
            1:$lang.realNameType.interface,
            2:$lang.realNameType.manual
        }
    },
    {
        id: 'certificateTime',
        title: '认证时间',
        type: 'date',
        format:'yyyy-MM-dd',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'remark',
        title: '审核备注',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
    },
    // {
    //     id: 'positiveIdcardPic',
    //     title: '身份证正面照片',
    //     type: 'string',
    //     headerClass: 'text-center',
    //     columnClass: 'text-center',
    //     fastSort: false,
    // },
    {
        id: 'verify',
        title: '审核',
        type: 'string',
        columnClass: 'text-center width-200',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var verifyHtml = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;审核&nbsp;</a>";
            var html = "";
            // 判断是否有审核权限
            if ($("#realAudit") && $("#realAudit").val() == 1) {
                if (record.authStatus == 1) {
                    html += verifyHtml;
                }
            }
            return html;
        }
    },
    {
        id: 'password',
        title: '查看',
        type: 'string',
        columnClass: 'text-center',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var look = "<a data-target='#lookModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;查看&nbsp;</a>";
            var html = "";
            // 判断是否有查看权限
            if ($("#realLook") && $("#realLook").val() == 1) {
                html += look;
            }
            return html;
        }
    },
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'real/list.json',
    exportFileName: '实名认证列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int:["id","realnameStatus","authStatus","userId","type"],Timestamp:["realnameVerifyTime","vipVerifyTime","certificateTime"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;

//排序
grid.sortParameter.columnId = ['asc_id'];
// grid.sortParameter.sortType = 1;

// setInterval(function () {
//     grid.refresh(true)
// }, 10000);

//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {

        //编辑对话框初始化
        $("#editModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#editForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];
            //审核页面数据
            $("#id").val(gridData.id);
            $("#userId").val(gridData.userId);
            $("#idcard").val(gridData.idcard);
            $("#realName").val(gridData.realname);
            $(".authStatus").bootstrapSwitch('state', true);
            $("#remark").val('');
        });

        //查看对话框初始化
        $("#lookModal").on("show.bs.modal", function (event) {
            var button = $(event.relatedTarget);
            var datano = button.data('no');
            var gridData = grid.sortOriginalDatas[datano];
            var modal = $(this);
            //审核页面数据
            modal.find('input[name="look_id"]').val(gridData.id);
            modal.find('input[name="look_userId"]').val(gridData.userId);
            modal.find('input[name="look_idcard"]').val(gridData.idcard);
            modal.find('input[name="look_realName"]').val(gridData.realname);
            modal.find('input[name="look_certificateTime"]').val(gridData.certificateTime);
            modal.find('input[name="look_remark"]').val(gridData.remark);
            if (gridData.authStatus == 1) {
                modal.find('input[name="look_authStatus"]').val("待审核");
            } else if (gridData.authStatus == 2) {
                modal.find('input[name="look_authStatus"]').val("通过");
            } else {
                modal.find('input[name="look_authStatus"]').val("未通过");
            }

        });

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = {};
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['lk_real_name'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }


    //外部可调用
    return {
        bindEvent: _bindEvent
    }
}();
//操作处理结束

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});